﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Finance.Bll;
using Finance.Entity;
using Finance.IBll;
using Finance.UI.Controllers;
using Finance.UI.Web;
using Finance.Utility;

namespace Finance.UI.Areas.FinanceManager.Controllers
{
    public class UserController : AreaBaseController
    {
        private IUserBll userBll = new UserBll();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Save(string userName, string password, string email,int orgId, int id = 0)
        {
            bool success = false;
            if (id <= 0)
            {

                success = userBll.CreateUser(userName, Encrypt.Md5(password), email, null, null,
                                             true,Guid.NewGuid().ToString(),orgId);
            }
            else
            {
                success = userBll.UpdateUser(id, null, email,orgId);
            }
            return Json(new { success });
        }



        public ActionResult List(string userName, int pageIndex = 1, int pageSize = 20)
        {
            int total = 0;
            IEnumerable<ViewUserEntity> userEntities = userBll.List(userName, pageIndex, pageSize, out total);

            return Json(new { success = true, data = userEntities, total });
        }

        public ActionResult ResetPassword(string userName, string password)
        {
      
            bool success = userBll.ResetPassword(userName, Encrypt.Md5(password));
            return Json(new { success, password });
        }

        public ActionResult Delete(int id)
        {
            bool success = userBll.DeleteUser(id);
            return Json(new { success });
        }

        public ActionResult ListUser(int orgId)
        {
            IEnumerable<ViewUserEntity> userEntities = userBll.List(orgId);
            return Json(new { success = true, data = userEntities });
        }

    }
}
